package com.fr.lintcode;

/**
*作者：furong
*日期：2016年12月28日
*时间：下午5:05:21
*/
public class Q38 {
    /**
     * @param matrix: A list of lists of integers
     * @param: A number you want to search in the matrix
     * @return: An integer indicate the occurrence of target in the given matrix
     */
    public int searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        if (m <= 0) {
            return 0;
        }
        int n = matrix[0].length;
        if (n <= 0) {
            return 0;
        }
        int a = 0;
        int b = 0;
        int count = 0;
        while (a < m) {
            if (b >= n) {
                a++;
                b = 0;
                continue;
            }
            if (matrix[a][b] == target) {
                a++;
                n = b;
                count++;
                b = 0;
            } else if (matrix[a][b] < target) {
                b++;
            } else {
                a++;
                n = b;
                b = 0;
            }
        }
        return count;
    }
}
